const app = getApp();
const api = require('../../ws_micro_shop/utils/api.js');


const handleStatisticsShare = options => {
  // 统计分享
  return new Promise((resolve, reject) => {
    const { type = '', param_id = '' } = options;
    app.ws_micro_shoprequest({
      url: api.staff.share_submit,
      data: {
        type,
        param_id,
      },
      method: 'post',
      success: res => {
        if (res && res.code === 0) {
          resolve(true);
        } else {
          reject(false);
        }
      },
      fail: res => {
        reject(false);
      },
    });
  });
};

const getIsRole = () => {
  // 获取角色信息
  return new Promise((resolve, reject) => {
    app.ws_micro_shoprequest({
      url: api.staff.get_is_role,
      success: res => {
        if (res && res.code === 0 && res.data) {
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      fail: res => {
        reject(res);
      },
    });
  });
};

const isAdminRole = async () => {
  // 获取是否为管理员
  try {
    const { role= '' } = await getIsRole();
    return Number(role) >= -1 ? true : false;
  } catch (error) {
    console.log('isAdmin error', error);
  }
};

const wxappjump = (appId,path,type,param_id,vm) => {
  vm.setData({
    share_modal_active: '',
  });
  wx.navigateToMiniProgram({
    appId,
    path,
    envVersion: 'release',
    success(res) {
      // 打开成功
      console.info('-------打开成功--------');
      handleStatisticsShare({type, param_id});
    }
  })
}

module.exports = {
  handleStatisticsShare,
  isAdminRole,
  getIsRole,
  wxappjump
};
